package com.cloud.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloud.common.domain.vo.RoutePlan;
import com.cloud.common.enums.RouteState;

import java.math.BigDecimal;
import java.util.HashMap;

/**
 * 高德/百度路径规划
 *
 * @author yzj
 */
public class RoutePlanUtils {

    /**
     * 获取高德线路规划信息
     *
     * @param lon1 开始坐标经度
     * @param lat1 开始坐标纬度
     * @param lon2 结束坐标经度
     * @param lat2 结束坐标纬度
     * @param ak   申请高德地图应用key
     * @return
     */
    public static RoutePlan getGoldRoutePlan(String lon1, String lat1, String lon2, String lat2, String ak) {
        RoutePlan routePlan = new RoutePlan();
        try {
            HashMap<String, Object> param = new HashMap<>();
            param.put("key", ak);
            param.put("origin", lon1 + "," + lat1);
            param.put("destination", lon2 + "," + lat2);
            param.put("extensions", "BASE");  // 返回基本信息
            param.put("strategy", 0); // 0速度优先,此路线不一定距离最短
            param.put("output", "JSON");
            String jsonResult = HttpUtil.get("https://restapi.amap.com/v3/direction/driving", param);
            JSONObject result = JSON.parseObject(jsonResult);
            // 判断是否发送成功
            if ("1".equals(result.getString("status"))) {
                JSONObject object = (JSONObject) result.get("route");
                JSONArray array = (JSONArray) object.get("paths");
                object = (JSONObject) array.get(0);
                int duration = Integer.parseInt(object.get("duration").toString());
                // 获取行驶时间,默认返回单位秒,现转小时
                BigDecimal decimal = new BigDecimal(duration / 3600f);
                routePlan.setState(RouteState.SUCCESS.getCode());
                routePlan.setRunTime(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            }
        } catch (Exception e) {
            routePlan.setState(RouteState.ERROR.getCode());
        }
        return routePlan;
    }

    /**
     * 获取百度线路规划信息
     *
     * @param lon1 开始坐标经度
     * @param lat1 开始坐标纬度
     * @param lon2 结束坐标经度
     * @param lat2 结束坐标纬度
     * @param ak   申请百度地图应用key
     * @return
     */
    public static RoutePlan getBaiduRoutePlan(String lon1, String lat1, String lon2, String lat2, String ak) {
        RoutePlan routePlan = new RoutePlan();
        try {
            HashMap<String, Object> param = new HashMap<>();
            param.put("origin", lat1 + "," + lon1);
            param.put("destination", lat2 + "," + lon2);
            param.put("ak", ak);
            // 返回数据类型
            String jsonResult = HttpUtil.get("https://api.map.baidu.com/directionlite/v1/driving", param);
            JSONObject result = JSON.parseObject(jsonResult);
            // 判断是否发送成功
            if ("0".equals(result.getString("status"))) {
                JSONObject object = (JSONObject) result.get("result");
                JSONArray array = (JSONArray) object.get("routes");
                object = (JSONObject) array.get(0);
                int duration = Integer.parseInt(object.get("duration").toString());
                // 获取行驶时间,默认返回单位秒,现转小时
                BigDecimal decimal = new BigDecimal(duration / 3600f);
                routePlan.setState(RouteState.SUCCESS.getCode());
                routePlan.setRunTime(decimal.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue());
            }
        } catch (Exception e) {
            routePlan.setState(RouteState.ERROR.getCode());
        }
        return routePlan;
    }

}
